<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket 聊天室测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .test-section {
            background: white;
            padding: 20px;
            margin: 20px 0;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        .test-button:hover {
            background: #0056b3;
        }
        .result {
            background: #f8f9fa;
            padding: 10px;
            border-radius: 5px;
            margin: 10px 0;
            border-left: 4px solid #007bff;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        .success {
            border-left-color: #28a745;
            background: #d4edda;
        }
    </style>
</head>
<body>
    <h1>WebSocket 聊天室测试页面</h1>
    
    <div class="test-section">
        <h2>1. 测试登录接口</h2>
        <button class="test-button" onclick="testLogin()">测试登录</button>
        <div id="loginResult" class="result" style="display: none;"></div>
    </div>
    
    <div class="test-section">
        <h2>2. 测试WebSocket连接</h2>
        <button class="test-button" onclick="testWebSocket()">测试WebSocket连接</button>
        <div id="wsResult" class="result" style="display: none;"></div>
    </div>
    
    <div class="test-section">
        <h2>3. 访问主应用</h2>
        <p>如果以上测试都通过，可以访问主应用：</p>
        <a href="http://localhost:8081" target="_blank" class="test-button" style="text-decoration: none; display: inline-block;">打开聊天室</a>
    </div>

    <script>
        function showResult(elementId, message, isError = false) {
            const element = document.getElementById(elementId);
            element.style.display = 'block';
            element.textContent = message;
            element.className = `result ${isError ? 'error' : 'success'}`;
        }

        async function testLogin() {
            try {
                const response = await fetch('http://localhost:8081/user/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: 'userName=testuser&passWord=123'
                });
                
                const result = await response.json();
                
                if (result.flag) {
                    showResult('loginResult', '登录测试成功！用户已登录。', false);
                } else {
                    showResult('loginResult', `登录测试失败：${result.message}`, true);
                }
            } catch (error) {
                showResult('loginResult', `登录测试失败：${error.message}`, true);
            }
        }

        function testWebSocket() {
            try {
                const ws = new WebSocket('ws://localhost:8081/chat');
                
                ws.onopen = function() {
                    showResult('wsResult', 'WebSocket连接测试成功！', false);
                    ws.close();
                };
                
                ws.onerror = function(error) {
                    showResult('wsResult', `WebSocket连接测试失败：${error}`, true);
                };
                
                ws.onclose = function() {
                    console.log('WebSocket连接已关闭');
                };
                
                // 5秒超时
                setTimeout(() => {
                    if (ws.readyState === WebSocket.CONNECTING) {
                        ws.close();
                        showResult('wsResult', 'WebSocket连接测试超时', true);
                    }
                }, 5000);
                
            } catch (error) {
                showResult('wsResult', `WebSocket连接测试失败：${error.message}`, true);
            }
        }
    </script>
</body>
</html>
